package idiet.wizards;

import idiet.Activator;
import idiet.bean.Customer;
import idiet.bean.CustomerPal;
import idiet.bean.Nation;
import idiet.bean.NurseTarget;
import idiet.service.CustomerService;
import idiet.wizards.page.WizardPageCustomerBasicInfo;
import idiet.wizards.page.WizardPageCustomerNurseInfo;
import idiet.wizards.page.WizardPageCustomerPhysiologicInfo;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.DialogSettings;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.wizard.Wizard;
import org.springframework.transaction.annotation.Transactional;

public class NewWizardAddCustomer extends Wizard {
	private static Logger logger=Logger.getLogger(NewWizardAddCustomer.class);
	private WizardPageCustomerBasicInfo basic;
	private WizardPageCustomerPhysiologicInfo physiologic;
	private WizardPageCustomerNurseInfo nurse;
	private CustomerService customerService=Activator.getApplicationContext().getBean(CustomerService.class);
	
	public NewWizardAddCustomer() {
		setWindowTitle("Add new customer");
	}

	@Override
	public void addPages() {
		basic=new WizardPageCustomerBasicInfo();
		physiologic=new WizardPageCustomerPhysiologicInfo();
		nurse=new WizardPageCustomerNurseInfo();
		addPage(basic);
		addPage(physiologic);
		addPage(nurse);
	}

	@Override
	@Transactional
	public boolean performFinish() {
		try {
			if(logger.isDebugEnabled())
					logger.debug("create customer start !");
			Customer customer=new Customer();
			DialogSettings settings=(DialogSettings) basic.getDialogSettings();
			customer.setName(settings.get("name"));
			customer.setGender(settings.get("gender"));
			
			Nation nation=new Nation();
			nation.setId(Integer.parseInt(settings.get("nation")));
			customer.setNation(nation);
			
			if(settings.get("phone").trim().equals("")){
				customer.setTelephone(null);
			}else{
				customer.setTelephone(settings.get("phone"));
			}
			if(settings.get("cellphone").trim().equals("")){
				customer.setCellphone(null);
			}else{
				customer.setCellphone(settings.get("cellphone"));
			}
			if(settings.get("im").trim().equals("")){
				customer.setIm(null);
			}else{
				customer.setIm(settings.get("im"));
			}
			if(settings.get("email").trim().equals("")){
				customer.setEmail(null);
			}else{
				customer.setEmail(settings.get("email"));
			}
			if(settings.get("address").trim().equals("")){
				customer.setAddress(null);
			}else{
				customer.setAddress(settings.get("address"));
			}
			if(settings.get("comment").trim().equals("")){
				customer.setBasicComment(null);
			}else{
				customer.setBasicComment(settings.get("comment"));
			}
			settings=(DialogSettings) physiologic.getDialogSettings();
			customer.setAge(settings.getInt("age"));
			customer.setHeight(new BigDecimal(settings.get("height")));
			customer.setWeight(new BigDecimal(settings.get("weight")));
			if(settings.get("waist").trim().equals("")){
				customer.setWaist(null);
			}else{
				customer.setWaist(new BigDecimal(settings.get("waist")));
			}
			if(settings.get("bust").trim().equals("")){
				customer.setBust(null);
			}else{
				customer.setBust(new BigDecimal(settings.get("bust")));
			}
			if(settings.get("hip").trim().equals("")){
				customer.setHips(null);
			}else{
				customer.setHips(new BigDecimal(settings.get("hip")));
			}
			customer.setPhysiologicComment(settings.get("comment"));
			settings=(DialogSettings) nurse.getDialogSettings();
			if(settings.get("pal").trim().equals("")){
				customer.setCustomerPal(null);
			}else{
				CustomerPal pal=new CustomerPal();
				pal.setId(settings.getInt("pal"));
				customer.setCustomerPal(pal);
			}
			if(settings.get("target").trim().equals("")){
				customer.setNurseTarget(null);
			}else{
				NurseTarget target=new NurseTarget();
				target.setId(settings.getInt("target"));
				customer.setNurseTarget(target);
			}
			if(settings.get("effect").trim().equals("")){
				customer.setEffect(null);
			}else{
				customer.setEffect(settings.get("effect"));
			}
			SimpleDateFormat format=new SimpleDateFormat("M/d/yyyy");
			customer.setForeStartDate(format.parse(StringUtils.substringBetween(settings.get("starttime"), "{", "}")));
			customer.setForeEndDate(format.parse(StringUtils.substringBetween(settings.get("endtime"), "{", "}")));
			if(settings.get("comment").trim().equals("")){
				customer.setNurseComment(null);
			}else{
				customer.setNurseComment(settings.get("comment"));
			}
			customerService.createCustomer(customer);
		} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				MessageDialog.openError(getShell(), "add customer information failed !", e.getMessage());
				return false;
		}
		return true;
	}
}
